//
//  NetworkSingleton.m
//  Builder
//
//  Created by JCK on 2017/12/5.
//  Copyright © 2017年 JCK. All rights reserved.
//

#import "NetworkSingleton.h"
#import "SVProgressHUD.h"
@implementation NetworkSingleton
+(NetworkSingleton *)sharedManager{
    static NetworkSingleton *sharedNetworkSingleton = nil;
    static dispatch_once_t predicate;
    dispatch_once(&predicate,^{
        sharedNetworkSingleton = [[self alloc] init];
    });
    return sharedNetworkSingleton;
}
-(AFHTTPSessionManager *)baseHtppRequest{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.requestSerializer setTimeoutInterval:TIMEOUT];

    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"text/html", @"application/json", nil];
    return manager;
}


#pragma mark - 获取数据
-(void)POSTdata:(NSDictionary *)parameters Path:(NSString *)path successBlock:(SuccessBlock)successBlock failureBlock:(FailureBlock)failureBlock{
    
    AFHTTPSessionManager *manager = [self baseHtppRequest];

    NSLog(@"path = %@ | parameters = %@",path,parameters);
    [manager POST:[NSString stringWithFormat:@"%@%@",ServerAddressURL,path] parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
         successBlock(responseObject);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSString *errorStr = [error.userInfo objectForKey:@"NSLocalizedDescription"];
        failureBlock(errorStr);
        ErrorShow(@"数据获取失败");
    }];
}

@end
